<?php
class core extends wenyan {
	public $db;
	public $ezr; //分页
	public $tbp;
	public $shopadmin_name;
	public $shopadmin_id;
	public $smarty;
	public $domain;
	public $act, $app;
	public $notify; //消息推送

	public function __construct() {
		parent::__construct();
		global $db, $ezr, $tbp,$smarty,$domain;
		global $notify;
		global $act, $app;
		$this->db = $db;
		$this->ezr = $ezr;
		$this->smarty = $smarty;
		$this->domain = $domain;
		$this->tbp = $tbp;
		$this->app = $app;
		$this->act = $act;
		//消息推送
		$this->notify = $notify;	
		if (in_array($act, array("login"))) {
			$this->shopadmin_id = 0;
			$this->shopadmin_name = '';
		} else {
			$this->check_login();
		}
		$this->_handle_log();
	}

	//是否登录
	public function _check_login() {
		if (isset($_SESSION['shopadmin']) 
			&& is_object($_SESSION['shopadmin']) 
			&& isset($_SESSION['shopadmin']->id) 
			&& intval($_SESSION['shopadmin']->id)>0 
		) {
			$this->shopadmin_id = $_SESSION['shopadmin']->id;
			$this->shopadmin_name = $_SESSION['shopadmin']->name;
			return true;
		} else {
			return false;
		}
	}

	//对是否登录函数的封装，如果登录了，则返回true，
	//否则，返回错误信息：-100，APP需检查此返回值，判断是否需要重新登录
	public function check_login(){
		if ($this->_check_login()) {
			return true;
		} else {
			//error('请登录',-100);
			header("Location: index.php?act=login");
		}
	}

	//显示
	public function display($tpl = '', $data=array()) {
		global $json;
		if ($tpl) {
			$template_file =$tpl;
		} else {
			$template_file = $this->app.".".$this->act.".html";
			if (!file_exists(TEMPLATE_PATH . '/' . $template_file)) {
				die("Template does not exists.");
			}
		}
		$this->smarty->assign("app", $this->app);
		$this->smarty->assign("act", $this->act);
		$this->smarty->assign("data", $data);	
		if (isset($_SESSION['shopadmin']) && is_object($_SESSION['shopadmin'])) {
			$this->smarty->assign("shopadmin", $_SESSION['shopadmin']);
		}
		$this->smarty->display($template_file);
		exit;
	}


	//设置分页的参数之类的
	public function setpages($data = array()) {
		$BRSR = (isset($_GET['BRSR']) && trim($_GET['BRSR'])) ? trim($_GET['BRSR']) : '';
		$this->smarty->assign('BRSR',$BRSR);	
		//$this->ezr->num_results_per_page=10;
		//$this->ezr->num_browse_links = 10;
		$this->ezr->set_qs_val('app',$this->app);
		$this->ezr->set_qs_val('act',$this->act);
		foreach ($data as $key=>$value) {
			$this->ezr->set_qs_val($key, $value);
			$this->smarty->assign($key, $value);
		}
	}

	//trigger error, exit
	public function error($msg, $msg_type=-1, $error=1) {
		global $json;
		$json['error'] = $error;
		$json['msg_type'] = $msg_type;
		$json['msg'] = $msg;
		$this->smarty->assign("error", $json);	
		$this->display('error.html');
	}

	private function _handle_log() {
		$log = m("log");
		$log->create();
	}
}
